<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *员工管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'主题列表'),
);
$setting=$this->plugin_setting('basic');
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'noshow'=>0,'status'=>0,'gamename'=>$this->plugin_name);
	if(isset($_RQ['type'])){
		$this->session('type',intval($_RQ['type']));
	}
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','merchid','mid','paixu','uniacid','title','logo','onlinetime','status','totop','type','gamename','createtime');
	$list=pdo_getslice('jy_merch_game_item',$params,array($pindex,$psize),$total,$fields,'','id desc');
	foreach($list as &$item){
		$item['member']=cfc('core')->member_show($item['mid']);
		$item['merchname']=pdo_getcolumn('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['merchid']),'merchname');
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>'预览中的帖子这里不显示！','edit'=>true,'list'=>array(
			array('name'=>'logo','title'=>'主题封面','type'=>'image'),
			array('name'=>'title','title'=>'主题名称','type'=>'string'),
			array('name'=>'member','title'=>'用户信息_Member','type'=>'member'),
			array('name'=>'gamename','title'=>'类型','type'=>'radio','options'=>'allgames2'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
		)),
		'search'=>array(
			array('name'=>'title','width'=>15,'title'=>'主题名称','type'=>'string'),
			array('name'=>'status','width'=>15,'title'=>'状态','type'=>'select','options'=>'status'),
		)
	);
	$return['status']=array(array('id'=>0,'title'=>'未显示'),array('id'=>1,'title'=>'显示'));
	$return['htmls']=$htmls;
	$return['allgames2']=$this->allgames2;
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$type=$this->session('type');
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_merch_game_item',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$item['onlinetime']=$item['onlinetime'];
		$item['content']=json_decode($item['content'],true);
		$sns = pdo_get('jy_quan_sns',array('id'=>$item['snsid'],'uniacid'=>$_SESSION['uniacid']));
		$item['pcd'] = json_decode($sns['pcd'],true);
	}else{
		$item=array('type'=>$type);
		$item['isxuni']=1;
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回主题列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('name'=>'title','title'=>'主题名称','type'=>'string'),
					array('name'=>'isxuni','title'=>'虚拟用户发布?','type'=>'switch'),
					array('name'=>'mid','title'=>'选择发布者','type'=>'select','show'=>array('isxuni'=>0),'options'=>'sysmids','option_value'=>'mid', 'help'=>'必须选择发布员，只能从平台维护员里面选择' ),
					//array('name'=>'paixu','title'=>'排序','type'=>'string'),
					//array('name'=>'logo','title'=>'主题封面','type'=>'image'),
					//array('name'=>'desc','title'=>'主题介绍','type'=>'text'),
					//array('name'=>'content','title'=>'主题内容','type'=>'richtext'),
					array('name'=>'pcd','title'=>'所属地址','type'=>'pcd'),
					array('name'=>'status','title'=>'状态','type'=>'switch'),
				)
			),
			array(
				'title'=>'主题内容',
					'list'=>array(
						array(
							'type'=>'array',
							'name'=>'content',
							'add_tip'=>'添加内容项',
								'list'=>array(
									array('type'=>'radio','name'=>'type','title'=>'类型','options'=>'types'),
									array('type'=>'string','name'=>'data','title'=>'内容','help'=>''),
								),
								'details'=>array(
									array('type'=>'radio','name'=>'type','title'=>'类型','options'=>'types'),
									array('type'=>'text','name'=>'data','title'=>'文本','show'=>array('type'=>1)),
									array('type'=>'image','name'=>'data','title'=>'单图','show'=>array('type'=>3)),
									array('type'=>'images','name'=>'data','title'=>'多图','show'=>array('type'=>10)),
									array('type'=>'video','name'=>'data','title'=>'视频','show'=>array('type'=>11)),
								)
						),
						array('name'=>'richtext','title'=>'富文本','type'=>'richtext'),
					)
			),
		)
	);
	$return['sysmids'] =$setting['sysmid'];
	$return['types']=array(1=>'文字',3=>'单图',10=>'多图',11=>'视频',100=>'富文本');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$id=intval($postdata['id']);
	if(!$postdata['title']){
		exi('请填写主题名称！','error');
	}
	if(!$postdata['isxuni'] && !$postdata['mid']){
		exi('请选择发布者！'.$postdata['mid'],'error');
	}
	if(!$postdata['content'] && !$postdata['richtext']){
		exi('请填写主题内容！','error');
	}
	//print_r($postdata['content']);exit;
	$pcd = $postdata['pcd'];
	if($postdata['isxuni'] && (!$pcd['province'] || !$pcd['city'] || !$pcd['district'])){
		exi('请完善圈所属地址！','error');
	}
	if($pcd['province'] && $pcd['city'] && $pcd['district']){	
		$postpcd = trim($pcd['province']).trim($pcd['city']).trim($pcd['district']);
	}
	$type=$this->session('type');
	if($postdata['isxuni']){
		$postdata['mid']=cfc('member')->rand_xuni();
		if(!$postdata['mid']){
			exi('没有虚拟用户！请先到：会员中心->会员列表->导入虚拟用户','error');
		}
	}
	if($type==6){
		if(!$postdata['wantmoney']){
			exi('请先设置充值金额！','error');
		}
	}
	if($type==2){
		if(!$postdata['wantmoney']){
			exi('请先设置充值金额！','error');
		}
		$fee=$postdata['wantmoney'];
		if($fee<=1){
			   exi('红包金额需大于1元','error');
		}
		if($fee>200){
			   exi('单个红包金额最多不超过200元','error');
		}
		if($postdata['rednum']<3 || $postdata['rednum']>20){
			 exi('红包个数介于（包括）3-20之间','error');
		}
		if($fee/$postdata['rednum']<=1){
			 exi('红包平均数，不能小于1元','error');
		}
	}
	$data=array(
		'title'=>trim($postdata['title']),
		'logo'=>trim($postdata['logo']),
		'content'=>ijson_encode($postdata['content']),
		'paixu'=>intval($postdata['paixu']),
		'status'=>intval($postdata['status']),
		'mid'=>intval($postdata['mid']),
		'type'=>$type,
		'rednum'=>intval($postdata['rednum']),
	);
	if($id){
		pdo_update('jy_merch_game_item',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=time();
		$data['onlinetime']=time()+7*24*3600;
		pdo_insert('jy_merch_game_item',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_merch_game_item',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='msgdel'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_merch_game_item_msg',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要编辑的主题！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_merch_game_item',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	if(isset($post['totop'])){
		pdo_update('jy_merch_game_item',array('totop'=>intval($post['totop'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}